package com.jackiechan;


import com.jackiechan.domain.Students;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyApp {
    public static void main(String[] args) throws IOException {
        // 访问mybatis 读取Student的数据
        // 1. 定义mybatis主配置文件的名称，从类路径的根开始（target/classes）
        String config = "mybatis.xml";
        // 2. 读取这个config表示的文件
        InputStream in  = Resources.getResourceAsStream(config);
        // 3. 创建SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 4. 创建SqlSessionFactory
        SqlSessionFactory factory = builder.build(in);
        // 5. 【重要】获取SqlSession
        SqlSession sqlSession = factory.openSession();
        // 6. 【重要】指定要执行sql语句的标识  sql映射文件中的namespace + "." + 标签的id值
        String sqlId = "com.jackiechan.dao.StudentsDao" + "." + "selectStudents";
        // 7. 【重要】执行sql语句，通过sqlId找到语句
        List<Students> students =sqlSession.selectList(sqlId);
        // 8. 输出结果
        students.forEach( stu -> System.out.println(stu));
        // 9. 关闭SqlSession对象
        sqlSession.close();
    }
}
